import os

KEY_FILE_NAME = "KEY_FILE_NAME"
KEY_ABS_PATH = "KEY_ABS_PATH"


"""
大批量修改指定目录下[含所有子目录]所有文件的文件名
即:将文件名中的某个子字符串进行替换
"""


def get_all_file_paths(directory_path: str) -> list:
    """
    递归获取指定目录下[含所有子目录]所有文件的绝对路径
    Parameters
    ----------
    directory_path : str
        目标根目录路径
    """
    if not os.path.isdir(directory_path):
        raise ValueError(f"路径不存在或不是目录:{directory_path}")

    file_path_objs = []
    for root, _, files in os.walk(directory_path):
        for name in files:
            obj = {}
            abs_path = os.path.abspath(os.path.join(root, name))
            obj[KEY_ABS_PATH] = abs_path
            obj[KEY_FILE_NAME] = name
            file_path_objs.append(obj)
    return file_path_objs


def filter_by_word(word: str, file_path_objs: list):
    result_list = []
    for obj in file_path_objs:
        if word in obj[KEY_FILE_NAME]:
            result_list.append(obj)
    return result_list


def generate_shell_cont(filter_objs, previous_substr, new_substr):
    for obj in filter_objs:
        absolute_path = str(obj[KEY_ABS_PATH]).replace(":", "")
        absolute_path = absolute_path.replace("\\", "/")

        new_path_str = absolute_path.replace(previous_substr, new_substr)
        cmdStr = f'mv "/{absolute_path}" "/{new_path_str}";\n'
        print(cmdStr)


# 示例用法
if __name__ == "__main__":
    directoryPath = r""
    filter_word = ".."

    previous_substr = ".."
    new_substr = "."

    path_objs = get_all_file_paths(directoryPath)
    filter_res = filter_by_word(filter_word, path_objs)
    generate_shell_cont(filter_res, previous_substr, new_substr)
